<?php

use think\facade\Db;
/**
 * 系统公用函数
 */

function show_p($data){
    echo "<pre>";
    print_r($data);
    exit;
}

/**
 * 根据项目id获取结构树
 * @param $project_id
 *
 */
function getProjectTreeData($project_id){
    if($project_id){
        $build_id = Db::name('mall_build')->where('project_id', $project_id)->where('del_date', 0)->value('id');
        if (empty($build_id)) {
            return ['code' => 0, 'msg' => '该项目没有建设单位'];
        } else {

            $buildfile = Db::name('mall_material')->where(['project_id' => $project_id, 'type' => 1, 'relation_id' => $build_id, 'is_file' => 0, 'parent_id' => 0])->field('id,name,number')->find();

            $list      = geTreeDataList($buildfile['id']);
            if (empty($list)) {
                return ['code' => 0, 'msg' => '未获取到数据'];
            } else {
                return ['code' => 1, 'msg' => '获取成功！', 'data' => $list];
            }
        }
        return ['code'=>1,'msg'=>'获取成功', 'data'=>$build_id];
    }else{
        return ['code'=>0,'msg'=>'未获取到项目ID', 'data'=>''];
    }
}

function geTreeDataList($parentId, $isshow = 1){
    $list = [];
    if ($parentId) {
        $data = Db::name('mall_material')->where(['parent_id' => $parentId, 'is_file' => 0,  ['number','<>', ''], ['sort','<>', 0]])->whereNotNull('number')->field('id,name,number,sort,project_id')->order('sort')->select();
        foreach ($data as $v) {
            $arr           = [];
            $arr['id'] = $v['id'];
            $arr['title']   =  $v['number'].$v['name'];
            $arr['number'] = $v['number'];
            $arr['node'] ='node';
            $arr['spread'] ='true';
            $arr["checked"]= false;
            if ($v['sort'] > 5 && $isshow == 1) {
                $list[] = [
                    'title'   => '施工文件',
                    'number' => '',
                    "spread"=> true,
                    'children'  => getConstruction($v['project_id'])
                ];
                $list[] = [
                    'title'   => '监理文件',
                    'number' => '',
                    "spread"=> true,
                    'children'  => getSupervisor($v['project_id'])
                ];
                $isshow = 0;
            }
            $lists = [];
            $lists = geTreeDataList($v['id'], 0);
                $arr['children'] = $lists;
                if($arr['children']){
                    $arr["status"]= 1;
                }else{
                    $arr["status"]= 0;
                }
            $list[] = $arr;
        }
    }
    return $list;
}


function getConstruction($projectId){
    $construction = Db::name('mall_construction')->where(['project_id' => $projectId, 'del_date' => 0,['sort','<>', 0]])->field('id,construction_name,contract_number,prefix,sort')->order('sort')->select();
    $list         = [];
    $list[]=[
        'title'=>'SXSS·4·1建设单位施工文件',
        'number'=>'SXSS·4·1',
        'contract_number'=>'',
        "status"=> 0,
    ];
    foreach ($construction as $v) {
        $arr           = [];
        $arr['title']   = $v['prefix'].$v['construction_name'];
        $arr['number'] = $v['prefix'];
        $arr['contract_number'] = $v['contract_number'];
        $arr["status"]= 0;
        $list[]        = $arr;
    }
    return $list;
}


function getSupervisor($projectId){
    $supervisor = Db::name('mall_supervisor')->where(['project_id' => $projectId, 'del_date' => 0,['sort','<>', 0]])->field('id,supervisor_name,contract_number,prefix,sort')->order('sort')->select();
    $list       = [];
    foreach ($supervisor as $v) {
        $arr           = [];
        $arr['title']   = $v['prefix'].$v['supervisor_name'];
        $arr['number'] = $v['prefix'];
        $arr['contract_number'] = $v['contract_number'];
        $arr["status"]= 0;
        $list[]        = $arr;
    }

    return $list;
}



/**
 * @descr: 获取案卷列表---检索
 * @date : 17:01 2023/12/5
 * @param $projectId
 * @param $reference
 * @return \think\Paginator
 * @throws DbException
 * @author: zhangY
 */
 function getRetrievalArchive($projectId, $reference, $page = 1,$searForm)
{
    $map =[];
    $where =[];

    if(isset($searForm['times']) && !empty($searForm['times'])){
        $times =explode(' - ',$searForm['times']);
        $where['start_time'] = ['>=',strtotime($times[0])];
        $where['end_time'] = ['<=',strtotime($times[1])];

    }
    if(isset($searForm['relation']) && !empty($searForm['relation'])){
        $relation =explode('-',$searForm['relation']);
        $where[] = ['type','=',$relation[0]];
        $where[] = ['relation_id','=',$relation[1]];
    }
    if(isset($searForm['duty']) && !empty($searForm['duty'])){
        $archives_id = Db::name('archives_file')->where('duty',$searForm['duty'])->column('archives_id');
        $where[] = ['id','in',$archives_id];
    }
    if(isset($searForm['status_stype']) && !empty($searForm['status_stype'])){
        $where[] = ['status','=',$searForm['status_stype']];
    }
    if(isset($searForm['term']) && !empty($searForm['term'])){
        $where[] = ['term','=',$searForm['term']];
    }
    if(isset($searForm['startdate']) && !empty($searForm['startdate'])){
        $where['start_time'] = ['>=',strtotime($searForm['startdate'])];
    }
    if(isset($searForm['enddate']) && !empty($searForm['enddate'])){
        $where['end_time'] = ['<=',strtotime($searForm['enddate'])];
    }
    if(isset($searForm['searchtype']) && $searForm['searchtype']==2){
        $ids = Db::name('archives_file')->whereLike('name','%'.$searForm['keyword'].'%')->column('archives_id');
        $where[] = ['id','in',$ids];
    }
    if(isset($searForm['searchtype']) && $searForm['searchtype']==1){
        $where[] = ['name','like','%'.$searForm['keyword'].'%'];
    }

    if(isset($searForm['searchtype']) && $searForm['searchtype']==4){
        // $where['key'] = $searForm['keywords'];
        $ids = Db::name('archives_file')->whereLike('name','%'.$searForm['keyword'].'%')->column('archives_id');

        $map['id'] = ['id','in',$ids];
        $where[] = ['name','like','%'.$searForm['keyword'].'%'];
//,'transfer_approval_type'=>2
        $total = Db::name('archives_admin')->where(['project_id' => $projectId, 'del_date' => 0])->whereor($map)->where($where)->whereNotNull('name')->count();

        $data  = Db::name('archives_admin')->where(['project_id' => $projectId, 'del_date' => 0])->whereor($map)->where($where)->whereNotNull('name')->order('reference,key')->page($page, 15)->select();
    }else{
        $total = Db::name('archives_admin')->where(['project_id' => $projectId, 'del_date' => 0])->whereor($map)->where('reference like "' . $reference . '%"')->where($where)->whereNotNull('name')->count();
        $data  = Db::name('archives_admin')->where(['project_id' => $projectId, 'del_date' => 0])->whereor($map)->where('reference like "' . $reference . '%"')->where($where)->whereNotNull('name')->order('reference,key')->page($page, 15)->select();
    }
    $data=$data->all();
    foreach($data as $k=>$v){
        $start = '';
        $end   = '';
        if($data[$k]['start_time']){
            $start = date('Ymd',$data[$k]['start_time']);
        }
        if($data[$k]['end_time']){
            $end  = date('Ymd',$data[$k]['end_time']);
        }
        $data[$k]['create_time'] = date('Y-m-d',$data[$k]['create_time']);

            $aurl ="https://sc.qichenglantai.com/static/uploads/documentation/".$data[$k]['reference']."-".$data[$k]['key']."卷.pdf";
            if($data[$k]['xmlUrl']){
                $data[$k]['xmlUrl'] ='https://sc.qichenglantai.com/'.$data[$k]['xmlUrl'];
            }else{
                $data[$k]['xmlUrl'] ="";
            }
        
        $data[$k]['aurl'] =$aurl;
        $data[$k]['reference'] =$v['reference'].'-'.$v['key'];

        $data[$k]['start_end_date'] = $start .'-'.$end;
        $data[$k]['keys'] =15*($page-1)+$k+1;
    }
    return ['data' => $data, 'total' => $total, 'searForm'=>$searForm];
}

